<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 学科传来的表头 -->
        <el-breadcrumb separator-class="el-icon-arrow-right" class="box" v-if="jump">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{this.subjectName}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索 -->
        <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
          <el-form-item label="标签名称">
            <el-input v-model="formInline.tagName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="state2">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm">清除</el-button>
            <el-button type="primary" @click="handleFilter">搜索</el-button>
          </el-form-item>
          <el-form-item style="float:right">
            <el-button type="text" icon="el-icon-back" v-if="jump" @click="jumpbtn">返回学科</el-button>
            <el-button type="success" @click="handleCreate" icon="el-icon-edit">新增标签</el-button>
          </el-form-item>
        </el-form>
        <!-- 数据条 -->
        <el-alert
          v-if="alertText !== ''"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 表格 -->
        <el-table highlight-current-row :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="100"></el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="150"></el-table-column>
          <el-table-column prop="tagName" label="标签名称" width="150"></el-table-column>
          <el-table-column prop="username" label="创建者" width="150"></el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="220">
            <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
          </el-table-column>
          <el-table-column prop="state" :formatter="formateFormOfEmployment" label="状态" width="120"></el-table-column>
          <el-table-column prop="name" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="disabledbtn(scope.row)">禁用</el-button>
              <el-button
                type="text"
                :disabled="scope.row.state ===1?true:false"
                @click="modification(scope.row)"
              >修改</el-button>
              <el-button
                slot="reference"
                :disabled="scope.row.state ===1?true:false"
                type="text"
                @click="del(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div style="float: right;margin-top: 10px;margin-bottom:10px">
          <PageTool
            :paginationPage="formInline.page"
            :paginationPagesize="formInline.pagesize"
            :total="total"
            @pageChange="handleCurrentChange"
            @pageSizeChange="handleSizeChange"
          ></PageTool>
        </div>
        <!-- 新增标签 -->
        <tagsAdd ref="editTags" @refresh="jumppage"></tagsAdd>
        <!-- 修改标签 -->
        <tagsMod ref="alterTags" @refresh="jumppage"></tagsMod>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags'
import PageTool from '@/module-manage/components/page-tool'
import tagsAdd from './../components/tags-add.vue'
import tagsMod from './../components/tags-modification.vue'
export default {
  data () {
    return {
      state2: '',
      formInline: {
        page: 1,
        pagesize: 10
      },
      subjectList: [], // 所属学科简单列表
      total: null,
      row: '',
      jump: false,
      currentPage4: 4,
      subjectName: '',
      tableData: [],
      alertText: '',
      disabled: true,
      display: '0',
      isShow: false,
      statedata: {
        id: '',
        state: ''
      }
    }
  },
  components: {
    PageTool, tagsAdd, tagsMod
  },
  created () {
    this.jumppage()
  },
  methods: {
    // 学科跳转
    jumppage (val) {
      if (this.$route.query.id) {
        this.jump = true
        this.formInline.subjectID = this.$route.query.id
        this.getList(this.formInline)
        this.subjectName = this.$route.query.subjectName
      } else {
        this.getList(val)
      }
    },
    // 搜索信息
    handleFilter () {
      this.formInline.page = 1
      if (this.state2 !== '') {
        this.formInline.state = this.state2
      }
      this.getList(this.formInline)
    },
    // 清除
    resetForm () {
      this.state2 = ''
      this.formInline.tagName = ''
    },
    // 分页
    handleSizeChange (val) {
      this.formInline.pagesize = val
      if (this.formInline.page === 1) {
        this.jumppage(this.formInline)
      }
    },
    handleCurrentChange (val) {
      this.formInline.page = val
      this.jumppage()
    },
    // 把聘用形式格式化为文字
    formateFormOfEmployment (row, column, cellValue, index) {
      return cellValue === 1 ? '已启用' : '已禁用'
    },
    // 禁用
    async disabledbtn (row) {
      this.statedata.id = row.id
      if (row.state === 1) {
        this.statedata.state = 0
      } else if (row.state === 0) {
        this.statedata.state = 1
      }
      await changeState(this.statedata)
      this.jumppage(this.formInline)
    },
    // 修改目录
    modification (row) {
      this.$refs.alterTags.add(row)
      this.row = row
    },
    // 获取列表数据
    async getList (val) {
      const { data } = await list(val)
      delete this.formInline.state
      this.tableData = data.items
      this.total = data.counts
      this.alertText = `共 ${this.total} 条记录`
      console.log(this.tableData)
    },
    // 新增目录
    handleCreate () {
      this.$refs.editTags.add()
    },
    // 删除数据
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.jumppage(this.formInline)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 返回学科
    jumpbtn () {
      this.$router.push('/subjects/list')
    }
  }
}
</script>

<style rel="stylesheet/scss" scoped lang='scss'>
.box {
  height: 30px;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 20px;
}
</style>
